// subcontract/Mine/order/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    tabList: [
      { text: '全部', id: 'all' },
      { text: '待付款', id: 'pay' },
      { text: '待收货', id: 'delivery' },
      { text: '已完成', id: 'finish' },
      { text: '已关闭', id: 'close' }
    ],
    active: 'all',
    page: 1,//页码
    pagingState: false,//分页状态
    orderList: [],//商品
    current: Date.now(),//当前时间戳
    payShow: false,//支付状态
    payItem: {},//支付商品
  },
  /**支付方式 */
  async unifiedpay(from, order_id) {
    let { payItem } = this.data
    console.log(payItem);
    let object = {
      order_id: payItem.id,
      from: 'order',
      pay_way: 1
    }
    const { code, data } = await getApp().api.unifiedpay(object)
    if (code == 1) {
      const ret = await getApp().requestPay(data)
      if (ret.errMsg == 'requestPayment:ok') {
        getApp().showToast('支付成功!')
        this.setData({
          page: 1,//页码
          pagingState: false,//分页状态
          orderList: [],//商品
          payShow: false,//支付状态
          payItem: {},//支付商品
        })
        this.getorderList()
      } else {
        getApp().showToast('支付失败！')
      }
    }
  },
  /**立即支付 */
  async payment(event) {
    console.log(event);
    let { item } = event.currentTarget.dataset
    this.setData({
      payShow: !this.data.payShow,
      payItem: item
    })

  },
  onClose() {
    this.setData({
      payShow: !this.data.payShow
    })
  },
  /**删除订单 */
  async orderDel(event) {
    let { id } = event.currentTarget.dataset
    console.log(event);
    let object = {
      id
    }
    const ret = await wx.showModal({
      title: '提示',
      content: '您确定要删除此订单嘛?',
    })
    if (ret.confirm) {
      const { code, daat } = await getApp().api.orderDel(object)
      if (code == 1) {
        this.setData({
          page: 1,//页码
          pagingState: false,//分页状态
          orderList: [],//商品
        })
        this.getorderList()
      }
    }

  },
  /**关闭订单 */
  async ordercancel(event) {
    let { id } = event.currentTarget.dataset
    console.log(event);
    let object = {
      id
    }
    const ret = await wx.showModal({
      title: '提示',
      content: '您确定要关闭此订单嘛?',
    })
    if (ret.confirm) {
      const { code, daat } = await getApp().api.ordercancel(object)
      if (code == 1) {
        this.setData({
          page: 1,//页码
          pagingState: false,//分页状态
          orderList: [],//商品
        })
        this.getorderList()
      }
    }
  },
  /**获取订单列表 */
  async getorderList() {
    let object = {
      type: this.data.active,
      page_no: this.data.page
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getorderList(object)
      if (code == 1) {
        this.setData({
          orderList: this.data.orderList.concat(data.list).map(item => {
            let count = 0
            item.order_goods.forEach((i) => {
              count += i.goods_num
            })
            item.count = count
            return item
          })
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })

      }
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  onChange(event) {
    console.log(event);
    this.setData({
      active: event.detail.name
    })
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      orderList: [],//商品
    })
    this.getorderList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getorderList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      orderList: [],//商品
    })
    this.getorderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})